#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "Administrator"
__name__="mtsleep3"
__date__="2016/9/18"
__time__="16:22"
"""
# 一：创建一个Thread的实例，传给它一个函数。
import threading
from time import sleep,ctime

loops = [4,2]

def loop(nloop,nsec):
    print 'start loop',nloop,'at:',ctime()
    sleep(nsec)
    print 'loop' ,nloop,'done at:',ctime()

def main():
    print 'starting at:',ctime()

    threads = []
    nloops = range(len(loops))

    for i in nloops:
        t = threading.Thread(target=loop,args=(i,loops[i])) # 把函数和参数传进去，得到返回的Thread实例。调用Thread()和调用thread.start_new_thread()最大区别就是新的线程不会立即开始。
        threads.append(t)
    for i in nloops:
        threads[i].start()
    for i in nloops:
        threads[i].join()   # join()函数运行主线程等待所有线程的结束。
    print 'all DONE at:',ctime()

if __name__ == '__main__':
    main()